package com.shangxia.monomer.study.designPatterns.structuralType.flyweight;

import lombok.extern.slf4j.Slf4j;

import java.util.HashMap;

/**
 * @ProjectName: monomer
 * @ClassName: FlyweightDemoSed
 * @Time: 2021/7/20
 * @Author: zhouy
 * @Description:
 * @Experience:
 */

@Slf4j
public class FlyweightDemoSed {
    /**
     * Flyweight：享元对象
     * IntrinsicState：内部状态，享元对象共享内部状态
     * ExtrinsicState：外部状态，每个享元对象的外部状态不同
     *
     * @param args
     */
    public static void main(String[] args) {
        FlyweightFactory factory = new FlyweightFactory();
        Flyweight flyweight1 = factory.getFlyweight("aa");
        Flyweight flyweight2 = factory.getFlyweight("aa");
        flyweight1.doOperation("x");
        flyweight2.doOperation("y");
    }

}

interface Flyweight {
    void doOperation(String extrinsicState);
}

class ConcreteFlyweight implements Flyweight {

    private String intrinsicState;

    public ConcreteFlyweight(String intrinsicState) {
        this.intrinsicState = intrinsicState;
    }

    @Override
    public void doOperation(String extrinsicState) {
        System.out.println("Object address: " + System.identityHashCode(this));
        System.out.println("IntrinsicState: " + intrinsicState);
        System.out.println("ExtrinsicState: " + extrinsicState);
    }
}

class FlyweightFactory {

    private HashMap<String, Flyweight> flyweights = new HashMap<>();

    Flyweight getFlyweight(String intrinsicState) {
        if (!flyweights.containsKey(intrinsicState)) {
            Flyweight flyweight = new ConcreteFlyweight(intrinsicState);
            flyweights.put(intrinsicState, flyweight);
        }
        return flyweights.get(intrinsicState);
    }
}
